﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotvaBot.Processor;

namespace BotvaBot
{
    public partial class UcPeriodicProcessing : UserControl
    {
        protected ILoggableProcessManager _ProcessManager = null;
        public ILoggableProcessManager ProcessManager
        {
            get { return _ProcessManager; }
            set
            {
                if (_ProcessManager != null)
                {
                    ProcessManager.SendLogMessage -= new EventHandler<LogMessageEventArgs>(ProcessManager_SendLogMessage);
                }

                _ProcessManager = value;

                if (_ProcessManager != null)
                {
                    ProcessManager.SendLogMessage += new EventHandler<LogMessageEventArgs>(ProcessManager_SendLogMessage);
                }
            }
        }

        public UcPeriodicProcessing()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            btnInit.Enabled = true;
            btnStart.Enabled = false;
            btnPause.Enabled = false;
            btnRestart.Enabled = false;
            btnStop.Enabled = false;
        }

        protected const int MAX_LOG_COUNT = -20;
        protected object lbxLog_lock = new object();
        protected void ProcessManager_SendLogMessage(object sender, LogMessageEventArgs e)
        {
            if (lbxLog.InvokeRequired)
                lbxLog.Invoke(new MethodInvoker(delegate() { ProcessManager_SendLogMessage(sender, e); }));
            else
            {
                lock (lbxLog_lock)
                {
                    if (MAX_LOG_COUNT > 0 && lbxLog.Items.Count >= MAX_LOG_COUNT)
                    {
                        while (lbxLog.Items.Count >= MAX_LOG_COUNT)
                        {
                            lbxLog.Items.RemoveAt(0);
                        }
                    }
                    string message = string.Format("[{0}]: {1}: {2}", e.TimeStamp.ToString("yyyy-MM-dd hh:mm:ss ms"), e.TypeCode, e.Message);
                    lbxLog.Items.Add(message);
                }
            }
        }


        private void btnInit_Click(object sender, EventArgs e)
        {
            if (ProcessManager != null)
            {
                PeriodicProcessorData data = new PeriodicProcessorData();
                data.StartAfterMSec = Int32.Parse(txtStartAfterMSec.Text);
                data.PeriodMSec = Int32.Parse(txtPeriodMSec.Text);
                data.RandomPauseMSec = Int32.Parse(txtRandomPauseMSec.Text);
                data.TimeQuantMSec = Int32.Parse(txtTimeQuantMSec.Text);
                data.WorkingTimeStart = dtpWorkingTimeStart.Value;
                data.WorkingTimeEnd = dtpWorkingTimeEnd.Value;
                data.MaxExecutingsPeriod = new TimeSpan(0, 0, 0, 0, Int32.Parse(txtSendingsLimitPeriodMSec.Text));
                data.MaxExecutingsInPeriod = Int32.Parse(txtMaxSendingsCount.Text);
                data.ReduceFirstTime = Int32.Parse(txtReduceFirstTime.Text);

                ProcessManager.Init(data);
            }

            btnInit.Enabled = true;
            btnStart.Enabled = true;
            btnPause.Enabled = false;
            btnRestart.Enabled = false;
            btnStop.Enabled = false;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (ProcessManager != null)
                ProcessManager.Start();

            btnInit.Enabled = false;
            btnStart.Enabled = false;
            btnPause.Enabled = true;
            btnRestart.Enabled = false;
            btnStop.Enabled = true;
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            if (ProcessManager != null)
                ProcessManager.Pause();

            btnInit.Enabled = true;
            btnStart.Enabled = false;
            btnPause.Enabled = false;
            btnRestart.Enabled = true;
            btnStop.Enabled = true;
        }

        private void btnRestart_Click(object sender, EventArgs e)
        {
            if (ProcessManager != null)
                ProcessManager.Restart();

            btnInit.Enabled = false;
            btnStart.Enabled = false;
            btnPause.Enabled = true;
            btnRestart.Enabled = false;
            btnStop.Enabled = true;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (ProcessManager != null)
                ProcessManager.Stop();

            btnInit.Enabled = true;
            btnStart.Enabled = true;
            btnPause.Enabled = false;
            btnRestart.Enabled = false;
            btnStop.Enabled = false;
        }
    }
}
